package models;

import java.util.Calendar;
import java.util.Date;

import com.google.api.client.auth.oauth2.Credential;
import com.google.api.services.datastore.DatastoreV1.Entity;
import com.google.api.services.datastore.DatastoreV1.Key;
import com.google.api.services.datastore.DatastoreV1.Property;
import com.google.api.services.datastore.DatastoreV1.Property.Builder;
import com.google.api.services.datastore.DatastoreV1.Value;
import com.google.api.services.datastore.client.Datastore;
import com.google.api.services.datastore.client.DatastoreFactory;
import com.google.api.services.datastore.client.DatastoreHelper;

import entities.IPRange;

public class DS_Config {

	// buildthewith
	public static String serviceEmail = "120936437457-ivb1d5qf7tisnp84ieue6g87jpmvqh92@developer.gserviceaccount.com";
	public static String serviceKeyFileName = "75c220c9fef5a0955a6563976fc9bf705f20d0f1-privatekey.p12";
	public static String serviceId = "s~build-again";

	public static Datastore datastore = null;

	static {
		Credential c;
		try {
			c = DatastoreHelper.getServiceAccountCredential(
					DS_Config.serviceEmail, DS_Config.serviceKeyFileName);
			datastore = DatastoreFactory
					.get()
					.create(DatastoreHelper.getOptionsfromEnv()
							.dataset(DS_Config.serviceId).credential(c).build());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static Datastore getDatastore() {
		return datastore;
	}

	public static Entity ipRangeToDatastoreEntity(IPRange obj) {
		Key.Builder key = Key.newBuilder().addPathElement(
				Key.PathElement.newBuilder().setKind("IPRange")
						.setName(obj.getId()));
		Entity datastoreEntity;
		Entity.Builder entityBuilder = Entity.newBuilder();

		entityBuilder.setKey(key.buildPartial());

		entityBuilder.addProperty(Property
				.newBuilder()
				.setName("id")
				.setValue(
						Value.newBuilder().setStringValue(obj.getId())
								.setIndexed(true)));
		entityBuilder.addProperty(Property
				.newBuilder()
				.setName("max")
				.setValue(
						Value.newBuilder().setDoubleValue(obj.getMax())
								.setIndexed(false)));

		entityBuilder.addProperty(Property
				.newBuilder()
				.setName("min")
				.setValue(
						Value.newBuilder().setDoubleValue(obj.getMin())
								.setIndexed(true)));
		entityBuilder.addProperty(Property
				.newBuilder()
				.setName("whois")
				.setValue(
						Value.newBuilder().setStringValue(obj.getWhois())
								.setIndexed(false)));
		datastoreEntity = entityBuilder.build();
		return datastoreEntity;
	}

	public static Entity objectToDatastoreEntity(Object obj, String objKey) {
		Key.Builder key = Key.newBuilder().addPathElement(
				Key.PathElement.newBuilder()
						.setKind(obj.getClass().getSimpleName())
						.setName(objKey));
		Entity datastoreEntity;
		Entity.Builder entityBuilder = Entity.newBuilder();

		entityBuilder.setKey(key.buildPartial());

		for (java.lang.reflect.Field f : obj.getClass().getDeclaredFields()) {
			String fName = f.getName();
			f.setAccessible(true);
			if (f.isAnnotationPresent(com.google.api.client.util.Key.class)) {
				continue;
			}
			if (f.getType().getSimpleName().equals("String")) {
				String fValue = "";
				try {
					if (f.get(obj) != null) {
						fValue = (String) f.get(obj);
					}
				} catch (Exception e) {
					e.printStackTrace();
					fValue = "null";
				}
				entityBuilder.addProperty(Property
						.newBuilder()
						.setName(fName)
						.setValue(
								Value.newBuilder().setStringValue(fValue)
										.setIndexed(false)));
			} else if (f.getType().getSimpleName().equals("int")) {
				int fValue = 0;
				try {
					if (f.get(obj) != null) {
						fValue = f.getInt(obj);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
				entityBuilder.addProperty(Property
						.newBuilder()
						.setName(fName)
						.setValue(
								Value.newBuilder().setIntegerValue(fValue)
										.setIndexed(false)));
			} else if (f.getType().getSimpleName().equals("Date")) {
				Date fValue = Calendar.getInstance().getTime();
				try {
					if (f.get(obj) != null) {
						fValue = (Date) f.get(obj);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
				Builder builder = Property
						.newBuilder()
						.setName(fName)
						.setValue(
								Value.newBuilder()
										.setTimestampMicrosecondsValue(
												fValue.getTime() * 1000)
										.setIndexed(false));
				entityBuilder.addProperty(builder);

			} else if (f.getType().getSimpleName().equals("boolean")) {
				boolean fValue = false;
				try {
					if (f.get(obj) != null) {
						fValue = (Boolean) f.get(obj);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
				entityBuilder.addProperty(Property
						.newBuilder()
						.setName(fName)
						.setValue(
								Value.newBuilder().setBooleanValue(fValue)
										.setIndexed(false)));
			} else if (f.getType().getSimpleName().equals("long")
					|| f.getType().getSimpleName().equals("Long")) {
				Double fValue = 0.0;
				try {
					if (f.get(obj) != null) {
						fValue = (Double) f.get(obj);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
				entityBuilder.addProperty(Property
						.newBuilder()
						.setName(fName)
						.setValue(
								Value.newBuilder().setDoubleValue(fValue)
										.setIndexed(false)));
			} else if (f.getType().getSimpleName().equals("double")
					|| f.getType().getSimpleName().equals("Double")) {
				Double fValue = 0.0;
				try {
					if (f.get(obj) != null) {
						fValue = (Double) f.get(obj);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
				entityBuilder.addProperty(Property
						.newBuilder()
						.setName(fName)
						.setValue(
								Value.newBuilder().setDoubleValue(fValue)
										.setIndexed(false)));
			} else if (f.getType().getSimpleName().equals("List")) {

			}
		}
		datastoreEntity = entityBuilder.build();
		return datastoreEntity;
	}
}